#include "Dot.h"
#include "MacroDef.h"
#include "MapIO.h"

Dot :: Dot(MapIO* io)
{
	this->io = io;
	x = 0;
	y = 0;
	vx = dotVx;
	vy = dotVy;

	dotImg = MapIO :: loadImg("pics\\dot.bmp");
	SDL_SetColorKey(dotImg, SDL_RLEACCEL|SDL_SRCCOLORKEY, SDL_MapRGB(dotImg->format, 0, 0xFF, 0xFF));
}

Dot :: ~Dot()
{
	SDL_FreeSurface(dotImg);
}

int Dot :: getX()
{
	return x;
}
int Dot :: getY()
{
	return y;
}

SDL_Surface* Dot :: getDotImg()
{
	return dotImg;
}

int Dot :: getVx()
{
	return vx;
}
void Dot :: moveLeft()
{
	x -= vx;
	if(x < 0)
		x += vx;
	setCamera();
}

void Dot :: moveRight()
{
	x += vx;
	if(x >=LEVEL_WIDTH)
		x -= vx;
	setCamera();
}

void Dot :: moveUp()
{
	y -= vy;
	if(y < 0)
		y += vy;
	setCamera();
}

void Dot :: moveDown()
{
	y += vy;
	if(y >= LEVEL_HEIGHT)
		y -= vy;
	setCamera();
}

void Dot :: setCamera()
{
	SDL_Rect* camera = io->getCamera();

	camera->x = (x + DOT_WIDTH / 2 ) - SCREEN_WIDTH / 2;
	camera->y = (y + DOT_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;

	//Keep the camera in bounds.
	if( camera->x < 0 )
	{
		camera->x = 0;    
	}
	if( camera->y < 0 )
	{
		camera->y = 0;    
	}
	if( camera->x > LEVEL_WIDTH - camera->w )
	{
		camera->x = LEVEL_WIDTH - camera->w;    
	}
	if( camera->y > LEVEL_HEIGHT - camera->h )
	{
		camera->y = LEVEL_HEIGHT - camera->h;    
	}    
}